import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

/**
 * 口算习题生成器：生成50道100以内加减法习题，保存到CSV文件
 * 规则：
 * 1. 加法：两个数均≤100，和≤100
 * 2. 减法：被减数≥减数，结果≥0，被减数≤100
 */
public class ExerciseGenerator {
    // CSV文件路径
    private static final String CSV_PATH = "math_exercises.csv";
    // 习题数量
    private static final int EXERCISE_COUNT = 50;
    // 随机数生成器
    private static final Random random = new Random();

    public static void main(String[] args) {
        generateExercises();
        System.out.println("50道口算习题已生成，保存至：" + CSV_PATH);
    }

    /**
     * 生成习题并写入CSV文件
     */
    public static void generateExercises() {
        try (FileWriter writer = new FileWriter(CSV_PATH)) {
            // 写入CSV表头
            writer.write("算式,答案\n");

            for (int i = 0; i < EXERCISE_COUNT; i++) {
                // 随机选择运算符：0=加法，1=减法
                int operatorType = random.nextInt(2);
                String expression;
                int answer;

                if (operatorType == 0) {
                    // 生成加法题：a + b = c，c ≤ 100
                    int a = random.nextInt(101);
                    // b的最大值为100 - a，确保和不超过100
                    int b = random.nextInt(101 - a);
                    answer = a + b;
                    expression = a + " + " + b + " = ?";
                } else {
                    // 生成减法题：a - b = c，c ≥ 0
                    int a = random.nextInt(101);
                    // b的最大值为a，确保结果非负
                    int b = random.nextInt(a + 1);
                    answer = a - b;
                    expression = a + " - " + b + " = ?";
                }

                // 写入CSV行（注意处理逗号转义，此处算式无逗号，可直接写入）
                writer.write(expression + "," + answer + "\n");
            }
        } catch (IOException e) {
            System.err.println("生成CSV文件失败：" + e.getMessage());
            e.printStackTrace();
        }
    }
}